import {Outlet, useLocation, useNavigate} from "react-router-dom";
import React, {useEffect, useState} from "react";
import {checkRouterAuth} from "../index";
import {getLocalStorage} from "../../utils";

//  /home路由守卫，权限管理等
const HomeGuard = () => {
    const navigate = useNavigate();
    const location = useLocation();
    const [auth, setAuth] = useState(false);
    useEffect(() => {
        let Loca = checkRouterAuth(location.pathname);
        let access_token = getLocalStorage('access_token');
        if (Loca && Loca.auth && !access_token) {
            navigate('/login', {replace: true });
            setAuth(false);
        } else {
            // if(!Loca || Loca.path === '/home'){
            //     navigate('/home/index', {replace: true });
            // }
            setAuth(true);
        }
    }, []);
    return auth ? <Outlet/> : null;
};
export default HomeGuard;
